import Vue from 'vue'
import Router from 'vue-router'
const Home = () => import('@/views/Home.vue')
const About = () => import('@/views/About.vue')
const Amusement = () => import('@/views/Amusement.vue')
const PartyHistory = () => import('@/views/PartyHistory.vue')
const Celebrity = () => import('@/views/Home/CardDetails.vue')
const Login = () => import('@/common/Login.vue')
const NoFind = () => import('@/common/404.vue')
const Register = () => import('@/common/Register.vue')
const UserInfo = () => import('@/views/UserInfo.vue')
const Modification = () => import('@/views/UserInfo/Modification.vue')
const ModificatonPassword = () =>
	import('@/views/UserInfo/ModificatonPassword.vue')
/* Admin 路由*/
const Admin = () => import('@/views/Admin.vue')
const Dashboard = () => import('@/views/Admin/Dashboard.vue')
const UserList = () => import('@/views/Admin/UserList.vue')
const MessageList = () => import('@/views/Admin/MessageList.vue')
const Roles = () => import('@/views/Admin/Roles.vue')
const Menu = () => import('@/views/Admin/Menu.vue')

const HomeCardInfo = () => import('@/views/Admin/HomeCardInfo.vue')
const HomeNotice = () => import('@/views/Admin/HomeNotice.vue')
const PartyHistoryTimeLine = () =>
	import('@/views/Admin/PartyHistoryTimeLine.vue')
const PartyHistoryLeaders = () =>
	import('@/views/Admin/PartyHistoryLeaders.vue')
const PartyHistoryQuestions = () =>
	import('@/views/Admin/PartyHistoryQuestions.vue')
const NotFind = () => import('@/views/Admin/404.vue')

Vue.use(Router)

/**
 * vue-router@3.0版本及以上回调形式已经改成promise api的形式了，返回的是一个promise，如果路由地址跳转相同,
 * 且没有捕获到错误，控制台始终会出现如图所示的警告 （注：3.0以下版本则不会出现以下警告！！！，因路由回调问题…）
 *
 */

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch((err) => err)
}

const routes = [
	{
		path: '/',
		redirect: '/home',
		component: Home,
	},
	{
		path: '/404',
		name: 'nofind',
		component: NotFind,
	},

	{
		path: '/login',
		name: 'login',
		component: Login,
	},
	{
		path: '/register',
		name: 'register',
		component: Register,
	},
	{
		path: '/home',
		name: 'home',
		component: Home,
	},
	{
		path: '/about',
		name: 'about',
		component: About,
	},
	{
		path: '/amusement',
		name: 'amusement',
		component: Amusement,
	},
	{
		path: '/partyHistory',
		name: 'partyHistory',
		component: PartyHistory,
	},
	{
		path: '/celebrity',
		name: 'celebrity',
		component: Celebrity,
	},
	{
		path: '/userInfo',
		name: 'userInfo',
		component: UserInfo,
	},
	{
		path: '/modification',
		name: 'modification',
		component: Modification,
	},
	{
		path: '/modificatonPassword',
		name: 'modificatonPassword',
		component: ModificatonPassword,
	},
	{
		path: '/admin',
		name: 'admin',
		component: Admin,
		redirect: '/admin/dashboard',
		meta: {
			title: 'admin',
		},
		children: [
			{
				path: 'dashboard',
				name: 'dashboard',
				component: Dashboard,
				meta: {
					title: '系统首页',
				},
			},

			{
				path: 'messageList',
				name: 'messageList',
				component: MessageList,
				meta: {
					title: '留言信息',
				},
			},
			{
				path: 'roles',
				name: 'roles',
				component: Roles,
				meta: {
					title: '角色管理',
				},
			},
			{
				path: 'users',
				name: 'users',
				component: UserList,
				meta: {
					title: '用户管理',
				},
			},
			{
				path: 'menus',
				name: 'menus',
				component: Menu,
				meta: {
					title: '菜单管理',
				},
			},
			{
				path: 'homeCardInfo',
				name: 'homeCardInfo',
				component: HomeCardInfo,
				meta: {
					title: '校园名人榜',
				},
			},
			{
				path: 'homeNotice',
				name: 'homeNotice',
				component: HomeNotice,
				meta: {
					title: '校园公告栏',
				},
			},
			{
				path: 'partyHistoryTimeLine',
				name: 'partyHistoryTimeLine',
				component: PartyHistoryTimeLine,
				meta: {
					title: '党的奋斗史',
				},
			},
			{
				path: 'partyHistoryLeaders',
				name: 'partyHistoryLeaders',
				component: PartyHistoryLeaders,
				meta: {
					title: '领导人展示',
				},
			},
			{
				path: 'partyHistoryQuestions',
				name: 'partyHistoryQuestions',
				component: PartyHistoryQuestions,
				meta: {
					title: '党史答题',
				},
			},
			{
				path: 'NotFind',
				name: 'NotFind',
				component: NotFind,
				meta: {
					title: '404页面PC',
				},
			},
			{
				path: 'NoFind',
				name: 'NoFind',
				component: NoFind,
				meta: {
					title: '404页面移动端',
				},
			},
		],
	},
	{
		path: '*', // 此处需特别注意置于最底部
		redirect: '/404',
	},
]

const router = new Router({
	// mode: "history",
	base: process.env.BASE_URL,
	routes,
})

router.beforeEach((to, from, next) => {
	let refused = [
		'/userInfo',
		'/modificatonPassword',
		'/admin',
		'/modification',
		'/admin/dashboard',
		'/admin/userList',
		'/admin/messageList',
		'/admin/roles',
		'/admin/homeCardInfo',
		'/admin/partyHistoryQuestions',
	]
	let refused2 = ['/userInfo', '/modificatonPassword']

	if (refused.includes(to.path)) {
		const tokenStr = window.sessionStorage.getItem('token')
		if (!tokenStr) {
			// 未登录访问权限路径
			return next('/login')
		}
		if (tokenStr) {
			// 登录状态 访问权限路径
			const admin = JSON.parse(window.sessionStorage.getItem('usersObj'))
			let promiseCode = ['admin', 'superAdmin']
			let bool = false
			for (let i = 0; i < admin.roles.length; i++) {
				if (promiseCode.includes(admin.roles[i].code)) {
					bool = true
					break
				}
			}

			if (!bool) {
				// 普通用户
				if (refused2.includes(to.path)) {
					// 访问普通路径
					return next()
				} else {
					// 访问后台
					return next('/home')
				}
			} else {
				//管理员级以上
				next()
			}
		}
	}
	next()
})

const createRouter = () =>
	new Router({
		mode: 'history',
		routes,
	})

const restRouter = createRouter()

export function resetRouter() {
	const newRouter = createRouter()
	router.matcher = newRouter.matcher // the relevant part
}

export default router
